<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>路由的嵌套</title>
    <script src="./lib/vue-2.6.12.js"></script>
    <script src="./lib/vue-router-3.5.1.js"></script>
</head>

<body>
    <div id="app">
        <router-view></router-view>
    </div>

    <template id=tmpl>
        <div>
            <h2>用户组件</h2>
            <router-link to="/user/profile">简况</router-link>
            <router-link to="/user/posts">邮件</router-link>
            <router-view></router-view>
        </div>
    </template>

    <script>
        const User = {
            template: '#tmpl'
        };

        const UserProfile = {
            template: '<h3>用户简况组件</h3>'
        }

        const UserPosts = {
            template: '<h3>用户邮件组件</h3>'
        }

        const UserHome = {
            template: '<h3>默认用户组件界面</h3>'
        }

        const router = new VueRouter({
            routes: [
                {
                    path: '/user', component: User,
                    children: [
                        {
                            // 当 /user/profile 匹配成功，
                            // UserProfile 会被渲染在 User 的 <router-view> 中
                            path: 'profile', component: UserProfile
                        },
                        {
                            // 当 /user/posts 匹配成功
                            // UserPosts 会被渲染在 User 的 <router-view> 中
                            path: 'posts', component: UserPosts
                        },
                        { 
                            // 当 /user 匹配时
                            // path: '', component: UserHome 
                            path: '', redirect: 'posts'
                        },
                    ]
                }
            ]
        });

        let vm = new Vue({
            el: '#app',
            router
        })
    </script>
</body>

</html>